package com.zabra.zl.ui.search;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.donkingliang.labels.LabelsView;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.ui.search.contract.SearchContract;
import com.zabra.zl.ui.search.presenter.SearchPresenter;
import com.zabra.zl.ui.search_list.SearchListActivity;
import com.zabra.zl.utils.BaseUtils;
import com.zabra.zl.utils.HistoryUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseMvpActivity<SearchPresenter> implements SearchContract.View {


    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.tv_delete)
    ImageView tvDelete;
    @BindView(R.id.rl_history)
    RelativeLayout rlHistory;
    @BindView(R.id.recycler_history)
    LabelsView recyclerHistory;
    private List<String> list = new ArrayList<>();
    private Adp adp;
    private Bundle bundle = new Bundle();
    private List<String> limitList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void initView() {
        list.add("中国电信");
        list.add("中国电信");
        list.add("中国电信");
        list.add("中国电信");
        list.add("中国电信");
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recycler.setNestedScrollingEnabled(false);
        adp = new Adp(R.layout.item_hot, list);
        recycler.setAdapter(adp);
        adp.openLoadAnimation();

        List<String> searchHistory = HistoryUtils.getSearchHistory();
        if (searchHistory.size() == 0 || searchHistory == null) {
            rlHistory.setVisibility(View.GONE);
        } else {
            //取前6条数据
            if (searchHistory.size() > 5) {
                limitList = searchHistory.subList(0, 6);
            } else {
                limitList = searchHistory;
            }
            recyclerHistory.setLabels(limitList, new LabelsView.LabelTextProvider<String>() {
                @Override
                public CharSequence getLabelText(TextView label, int position, String s) {
                    return s;
                }
            });
            recyclerHistory.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
                @Override
                public void onLabelClick(TextView label, Object data, int position) {
                    bundle.putString("content", limitList.get(position));
                    toClass(SearchActivity.this, SearchListActivity.class, bundle);
                }
            });
        }

        //键盘搜索键
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    String strSearch = etSearch.getText().toString().trim();
                    if (!TextUtils.isEmpty(strSearch)) {
                        BaseUtils.hideInput(SearchActivity.this);
                        HistoryUtils.saveSearchHistory(strSearch);
                        bundle.putString("content", strSearch);
                        toClass(SearchActivity.this, SearchListActivity.class, bundle);
                    }

                }
                return false;
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }


    @OnClick({R.id.tv_cancel, R.id.tv_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_delete:
                HistoryUtils.clearData();
                rlHistory.setVisibility(View.GONE);
                recyclerHistory.setVisibility(View.GONE);
                break;
        }
    }
}
